namespace SeataNet.Core.Configuration
{
    /// <summary>
    /// Seata配置类
    /// </summary>
    public class SeataConfiguration
    {
        private static readonly Lazy<SeataConfiguration> _instance = new Lazy<SeataConfiguration>(() => new SeataConfiguration());

        /// <summary>
        /// 获取配置单例实例
        /// </summary>
        public static SeataConfiguration Instance => _instance.Value;

        /// <summary>
        /// 应用ID
        /// </summary>
        public string ApplicationId { get; set; } = "seata-net-app";
        
        /// <summary>
        /// 事务服务组
        /// </summary>
        public string TransactionServiceGroup { get; set; } = "my_test_tx_group";

        /// <summary>
        /// 事务服务组别名（用于兼容性）
        /// </summary>
        public string TxServiceGroup
        {
            get => TransactionServiceGroup;
            set => TransactionServiceGroup = value;
        }

        /// <summary>
        /// TC服务器地址
        /// </summary>
        public string TcServerAddress { get; set; } = "127.0.0.1:8091";
        
        /// <summary>
        /// 事务超时时间（毫秒）
        /// </summary>
        public int TransactionTimeout { get; set; } = 60000;
        
        /// <summary>
        /// 是否启用日志
        /// </summary>
        public bool EnableLog { get; set; } = true;
        
        /// <summary>
        /// 日志级别
        /// </summary>
        public string LogLevel { get; set; } = "INFO";
        
        /// <summary>
        /// 序列化方式
        /// </summary>
        public string Serializer { get; set; } = "json";
        
        /// <summary>
        /// 压缩方式
        /// </summary>
        public string Compressor { get; set; } = "none";
        
        /// <summary>
        /// 是否启用性能监控
        /// </summary>
        public bool EnableMetrics { get; set; } = false;
        
        /// <summary>
        /// 注册中心类型
        /// </summary>
        public string RegistryType { get; set; } = "file";
        
        /// <summary>
        /// 配置中心类型
        /// </summary>
        public string ConfigType { get; set; } = "file";
        
        /// <summary>
        /// 数据库代理配置
        /// </summary>
        public DatabaseProxyConfig DatabaseProxy { get; set; } = new DatabaseProxyConfig();
        
        /// <summary>
        /// 传输配置
        /// </summary>
        public TransportConfig Transport { get; set; } = new TransportConfig();

        /// <summary>
        /// 更新配置实例
        /// </summary>
        /// <param name="configuration">新的配置对象</param>
        public void Update(SeataConfiguration configuration)
        {
            if (configuration == null)
                throw new ArgumentNullException(nameof(configuration));

            ApplicationId = configuration.ApplicationId;
            TransactionServiceGroup = configuration.TransactionServiceGroup;
            TcServerAddress = configuration.TcServerAddress;
            TransactionTimeout = configuration.TransactionTimeout;
            EnableLog = configuration.EnableLog;
            LogLevel = configuration.LogLevel;
            Serializer = configuration.Serializer;
            Compressor = configuration.Compressor;
            EnableMetrics = configuration.EnableMetrics;
            RegistryType = configuration.RegistryType;
            ConfigType = configuration.ConfigType;
            DatabaseProxy = configuration.DatabaseProxy;
            Transport = configuration.Transport;
        }
    }
    
    /// <summary>
    /// 数据库代理配置
    /// </summary>
    public class DatabaseProxyConfig
    {
        /// <summary>
        /// 数据源
        /// </summary>
        public string DataSource { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; } = string.Empty;
        
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; } = string.Empty;
        
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = true;
        
        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DbType { get; set; } = "mysql";
        
        /// <summary>
        /// 驱动类名
        /// </summary>
        public string DriverClassName { get; set; } = string.Empty;
        
        /// <summary>
        /// 连接URL
        /// </summary>
        public string Url { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string Username { get; set; } = string.Empty;
        
        /// <summary>
        /// 最大连接池大小
        /// </summary>
        public int MaxPoolSize { get; set; } = 100;
        
        /// <summary>
        /// 最小连接池大小
        /// </summary>
        public int MinPoolSize { get; set; } = 5;
        
        /// <summary>
        /// 连接超时时间（秒）
        /// </summary>
        public int ConnectionTimeout { get; set; } = 30;
    }
    
    /// <summary>
    /// 传输配置
    /// </summary>
    public class TransportConfig
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string ServerAddress { get; set; } = "127.0.0.1:8091";

        /// <summary>
        /// 监听地址
        /// </summary>
        public string ListenAddress { get; set; } = "0.0.0.0:8091";

        /// <summary>
        /// 传输类型
        /// </summary>
        public string Type { get; set; } = "TCP";

        /// <summary>
        /// 超时时间
        /// </summary>
        public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30);

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryAttempts { get; set; } = 3;

        /// <summary>
        /// 重试间隔（毫秒）
        /// </summary>
        public int RetryInterval { get; set; } = 1000;

        /// <summary>
        /// 重试策略类型
        /// </summary>
        public string RetryPolicyType { get; set; } = "Fixed";

        /// <summary>
        /// 熔断器失败阈值
        /// </summary>
        public int CircuitBreakerFailureThreshold { get; set; } = 5;

        /// <summary>
        /// 熔断器恢复超时时间（毫秒）
        /// </summary>
        public int CircuitBreakerRecoveryTimeout { get; set; } = 60000;

        /// <summary>
        /// 序列化器类型
        /// </summary>
        public string SerializerType { get; set; } = "json";

        /// <summary>
        /// 负载均衡器类型
        /// </summary>
        public string LoadBalancerType { get; set; } = "RoundRobin";

        /// <summary>
        /// 服务器NIO线程数
        /// </summary>
        public int ServerNioThreads { get; set; } = 1;

        /// <summary>
        /// 客户端Boss线程数
        /// </summary>
        public int ClientBossThreads { get; set; } = 1;

        /// <summary>
        /// 客户端工作线程数
        /// </summary>
        public int ClientWorkerThreads { get; set; } = 1;

        /// <summary>
        /// 是否启用心跳
        /// </summary>
        public bool EnableHeartbeat { get; set; } = true;

        /// <summary>
        /// 心跳间隔（毫秒）
        /// </summary>
        public int HeartbeatInterval { get; set; } = 15000;

        /// <summary>
        /// 序列化方式
        /// </summary>
        public string Serialization { get; set; } = "json";

        /// <summary>
        /// 压缩方式
        /// </summary>
        public string Compressor { get; set; } = "none";

        /// <summary>
        /// 序列化线程池核心大小
        /// </summary>
        public int SerializationThreadPoolCoreSize { get; set; } = 1;

        /// <summary>
        /// 序列化线程池最大大小
        /// </summary>
        public int SerializationThreadPoolMaxSize { get; set; } = 1;

        /// <summary>
        /// 是否启用TCC
        /// </summary>
        public bool EnableTccServer { get; set; } = false;

        /// <summary>
        /// 关闭等待时间（秒）
        /// </summary>
        public int ShutdownWait { get; set; } = 3;

        /// <summary>
        /// RPC请求超时时间（毫秒）
        /// </summary>
        public int RpcRequestTimeout { get; set; } = 30000;
    }

    /// <summary>
    /// SeataNet配置（别名）
    /// </summary>
    public class SeataNetConfiguration : SeataConfiguration
    {
    }

    /// <summary>
    /// AT模式配置
    /// </summary>
    public class AtConfiguration
    {
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// Undo日志表名
        /// </summary>
        public string UndoLogTable { get; set; } = "undo_log";

        /// <summary>
        /// Undo日志保留天数
        /// </summary>
        public int UndoLogRetentionDays { get; set; } = 7;
    }

    /// <summary>
    /// RPC配置
    /// </summary>
    public class RpcConfiguration
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string ServerAddress { get; set; } = "127.0.0.1:8091";

        /// <summary>
        /// 超时时间（毫秒）
        /// </summary>
        public int Timeout { get; set; } = 30000;
    }
}